<%@ include file="import_page.jsp"%>
<%
	boolean loginFail = false;
	if (request.getMethod() == "POST") {

		String loginName = request.getParameter(PAR_LOGIN_NAME);
		String password = request.getParameter(PAR_PASSWORD);
		//post back
		if (!(isEmptyString(loginName) || isEmptyString(password))) {
			//login process
			try {
				UserControl uCon = new UserControl();
				loginFail = !uCon.login(loginName, password);
				//close the database connection
				uCon.closeDBConnection();

				//If login success
				if (!loginFail) {
					//login success
					Users loginUser = uCon.getLoginUser();
					session.setAttribute(SESSION_LOGIN_USER, loginUser);
					session.removeAttribute(SESSION_SELECT_UNIT);
					String redirURL;
					switch (loginUser.getUserType()) {
						case admin :
							redirURL = URL_ADMIN;
							break;
						case staff :
							redirURL = URL_STAFF;
							break;
						default :
							redirURL = URL_STUDENT;
					}
					//send Redirect
					response.sendRedirect(redirURL);
					return; //end of this page, if login success
				}
			} catch (Exception err) {
				//if error, show login fail
				loginFail = true;
			}
		} else {
			//login fail
			//mark login fail for print error message
			loginFail = true;
			//clear all login record
			session.invalidate();
		}

	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Login</title>
<style type="text/css">
<!--
.style1 {
	font-size: large;
	font-weight: bold;
}

body {
	background-image: url(images/UniLearnSystem_bg.jpg);
}
-->
</style>
<script type="text/javascript" src="js/default.js"></script>
</head>

<body>
	<form id="login_form" action="f_login.jsp" method="post">
		<table width="100%" height="125" border="1" bordercolor="#CCCCCC"
			bgcolor="#00FF99">
			<tr>
				<td height="63" colspan="2"><div align="center">
						<span class="style1">Student / Staff Login </span>
					</div>
				</td>
			</tr>
			<tr>
				<td width="53" height="26"><strong>Name:</strong>
				</td>
				<td width="296"><label> <input name="login_name"
						type="text" id="login_name" /> </label>
				</td>
			</tr>
			<tr>
				<td height="26"><strong>Password: </strong>
				</td>
				<td><label> <input name="password" type="password"
						id="password" /> </label>
				</td>
			</tr>
			<%
				if (loginFail) {
			%>
			<tr>
				<td colspan="2"><div align="center"><%=ERR_LOGIN_FAIL%></div></td>
			</tr>
			<%
				}
			%>
		</table>
		<p align="center">
			<!-- <a href="javascript: submitform('login_form');"><img src="images/button_login.gif" alt="login" width="323" height="112" border="0" /></a> -->
			<input type="image" src="images/button_login.gif" alt="login"
				width="323" height="112" />
		</p>
	</form>

</body>
</html>
